<?php
/**
 * Copyright                [HD框架] (C)2011-2012 houdunwang ，Inc.
 * Encoding                 UTF-8
 * @author                  向军
 * Link                     http://www.hdphp.com
 * E-mail                   houdunwangxj@gmail.com
 */
class CategoryControl extends Control{
    function __auto(){
        C("debug",0);
    }
    //显示栏目列表
    function index(){
        $db  = M("category");
        $total = $db->count(); //获得栏目总数
        $page = new page($total,13); //设置分页每13条记录为一页
        $data = $db->limit($page->limit())->all();
        $this->assign("data",$data);
        $this->assign('page',$page->show(2));
        $this->display();
    }
    // 异步验证顶级栏目名称是否存在
    function checkcategory(){
        $name=$_POST['name'];
        $db=M('category');
        if($db->find("catname='$name'")){
            echo json_encode(array('status' => 1));
        }else{
            echo json_encode(array('status' => 0));
        }
    }
    //添加栏目视图
    function addshow(){
        $this->display();
    }
    //添加子栏目视图
    function addshow1(){
        $cid=$_GET['cid'];
        $this->assign('cid',$cid);
        $this->display();
    }
    //写入数据库中
    function add(){
        $db = M("category");
        if($db->add()){
            $this->success("栏目添加成功",'index');
        }else{
            $this->error("添加栏目失败");
        }
    }
    //编辑栏目视图
    function editshow(){
        $cid=$this->_get('cid');
        if($cid){
            $db = M("category");
            $field = $db->find($cid);
            $this->assign("field", $field); //分配栏目数据
            $this->display();
        }
    }

    //修改(编辑)栏目
    function save(){
        $db = M("category");
        if ($db->save()) {
            $this->success("编辑栏目成功", "index");
        } else {
            $this->error("栏目修改失败");
        }
    }

    //删除栏目
    function delcategory() {
        $cid = $this->_get("cid");
        if ($cid) {
            $db = M("category");
            $data = array(); //返回客户端的数据
            if ($db->find("pid=" . $cid)) {
                $data['stat'] = "error";
                $data['msg'] = "请删除子栏目";
            }else if ($db->table('arc')->find("cid=" . $cid)) {
                $data['stat'] = "error1";
                $data['msg'] = "请先移除栏目内文章";
            } else {
                $db->del($cid);
                $data['stat'] = "success";
                $data['msg'] = "";
            }
            echo json_encode($data);exit;
        }
    }
}

?>